﻿Imports DTO
Imports System.Data.SqlClient

Public Class HangHoaDAO
    Public Shared Function LoadBangHangHoa() As DataTable
        Return CKetNoi.LoadBang("select MaHangHoa, TenHangHoa from HangHoa ")
    End Function

    Public Shared Function LayDSLoaiHHCbx() As DataTable
        Return CKetNoi.GetData("select Ma, TenLoaiHangHoa from LoaiHangHoa ")
    End Function


    Public Shared Function LayDSHangHoa() As DataTable
        Return CKetNoi.GetData("LayDSHangHoa")
    End Function

    Public Shared Function ThemHangHoa(hh As HangHoaDTO) As Integer
        Dim kt As Integer
        Dim lst As New List(Of SqlParameter)
        lst.Add(New SqlParameter("@tenHH", hh.TenHangHoa))
        lst.Add(New SqlParameter("@cc", hh.MaNhaCungCap))
        lst.Add(New SqlParameter("@nh", hh.MaNhomHang))
        lst.Add(New SqlParameter("@lhh", hh.MaLoaiHangHoa))
        lst.Add(New SqlParameter("@dv", hh.MaDonVi))
        lst.Add(New SqlParameter("@kh", hh.MaKhoHang))
        lst.Add(New SqlParameter("@xx", hh.XuatXu))
        lst.Add(New SqlParameter("@anh", hh.DiaChiAnh))
        lst.Add(New SqlParameter("@tktt", hh.TonKhoToiThieu))
        lst.Add(New SqlParameter("@tkht", hh.TonKhoHienTai))
        lst.Add(New SqlParameter("@gm", hh.GiaMua))
        lst.Add(New SqlParameter("@gbl", hh.GiaBanLe))
        lst.Add(New SqlParameter("@gbs", hh.GiaBanSi))
        lst.Add(New SqlParameter("@matt", hh.MaTrangThai))
        kt = CKetNoi.ExecQueryStoreProc("ThemHangHoa", lst)
        Return kt
    End Function

    Public Shared Function CapNhatHangHoa(hh As HangHoaDTO) As Integer
        Dim kt As Integer
        Dim lst As New List(Of SqlParameter)
        lst.Add(New SqlParameter("@mahh", hh.MaHangHoa))
        lst.Add(New SqlParameter("@tenHH", hh.TenHangHoa))
        lst.Add(New SqlParameter("@cc", hh.MaNhaCungCap))
        lst.Add(New SqlParameter("@nh", hh.MaNhomHang))
        lst.Add(New SqlParameter("@lhh", hh.MaLoaiHangHoa))
        lst.Add(New SqlParameter("@dv", hh.MaDonVi))
        lst.Add(New SqlParameter("@kh", hh.MaKhoHang))
        lst.Add(New SqlParameter("@xx", hh.XuatXu))
        lst.Add(New SqlParameter("@anh", hh.DiaChiAnh))
        lst.Add(New SqlParameter("@tktt", hh.TonKhoToiThieu))
        lst.Add(New SqlParameter("@tkht", hh.TonKhoHienTai))
        lst.Add(New SqlParameter("@gm", hh.GiaMua))
        lst.Add(New SqlParameter("@gbl", hh.GiaBanLe))
        lst.Add(New SqlParameter("@gbs", hh.GiaBanSi))
        lst.Add(New SqlParameter("@matt", hh.MaTrangThai))
        kt = CKetNoi.ExecQueryStoreProc("CapNhatHangHoa", lst)
        Return kt
    End Function


    Public Shared Function XoaHangHoa(hh As HangHoaDTO) As Integer
        Dim kt As Integer
        Dim lst As New List(Of SqlParameter)
        lst.Add(New SqlParameter("@mahh", hh.MaHangHoa))
        kt = CKetNoi.ExecQueryStoreProc("XoaHangHoa", lst)
        Return kt
    End Function

    Public Shared KetNoi As SqlConnection = CKetNoi.Connect

    Public Shared Sub LayMaHangHoaCbx(ByVal mahh As String, ByVal lhh As Integer, ByVal kmd As String, ByVal nh As String, ByVal dv As String, ByVal ncc As String)

        KetNoi.Open()

        Dim cmd As New SqlCommand
        cmd.CommandType = CommandType.StoredProcedure
        cmd.CommandText = "LayMaHangHoaCbx"
        cmd.Connection = KetNoi


        Dim para As SqlParameter

        para = New System.Data.SqlClient.SqlParameter("maHH", SqlDbType.VarChar, 7)
        para.Direction = ParameterDirection.Input
        para.Value = mahh
        cmd.Parameters.Add(para)

        para = New System.Data.SqlClient.SqlParameter("ncc", SqlDbType.Int)
        para.Direction = ParameterDirection.Output
        cmd.Parameters.Add(para)

        para = New System.Data.SqlClient.SqlParameter("nh", SqlDbType.VarChar, 7)
        para.Direction = ParameterDirection.Output
        cmd.Parameters.Add(para)

        para = New System.Data.SqlClient.SqlParameter("lhh", SqlDbType.VarChar, 7)
        para.Direction = ParameterDirection.Output
        cmd.Parameters.Add(para)

        para = New System.Data.SqlClient.SqlParameter("dv", SqlDbType.VarChar, 7)
        para.Direction = ParameterDirection.Output
        cmd.Parameters.Add(para)

        para = New System.Data.SqlClient.SqlParameter("kh", SqlDbType.VarChar, 7)
        para.Direction = ParameterDirection.Output
        cmd.Parameters.Add(para)

        

        cmd.ExecuteNonQuery()

        lhh = cmd.Parameters("lhh").Value
        kmd = cmd.Parameters("kh").Value
        nh = cmd.Parameters("nh").Value
        dv = cmd.Parameters("dv").Value
        ncc = cmd.Parameters("ncc").Value

        KetNoi.Close()

    End Sub


End Class
